﻿CREATE FUNCTION [util].[GetWeekDaysFromBitmask]
(@Bitmask INT)
RETURNS NVARCHAR (128)
AS
BEGIN
	DECLARE @ReturnValue NVARCHAR(128);

	SELECT @ReturnValue = SUBSTRING(
		(CASE (@Bitmask & POWER(2,0)) WHEN 0 THEN '' ELSE ',Monday' END)
		+ (CASE (@Bitmask & POWER(2,1)) WHEN 0 THEN '' ELSE ',Tuesday' END)
		+ (CASE (@Bitmask & POWER(2,2)) WHEN 0 THEN '' ELSE ',Wednesday' END)
		+ (CASE (@Bitmask & POWER(2,3)) WHEN 0 THEN '' ELSE ',Thursday' END)
		+ (CASE (@Bitmask & POWER(2,4)) WHEN 0 THEN '' ELSE ',Friday' END)		                                                                  
		+ (CASE (@Bitmask & POWER(2,5)) WHEN 0 THEN '' ELSE ',Saturday' END)
		+ (CASE (@Bitmask & POWER(2,6)) WHEN 0 THEN '' ELSE ',Sunday' END)
	,2,128);
		
	RETURN @ReturnValue
END